const app = getApp()
Page({
  /**
    * 页面的初始数据
    */
  data: {
    show: false,
    ucode_user: {face:'/tab/img/nickname.png',nickname:'匿名'}
  },
  
  //页面创建时执行
  onLoad: function (options) {
    app.getParam(options,()=>{
      this.getUcode();
    });
  },
  //获取邀请用户
  getUcode: function () {
    if (!app.util().isNull(app.globalData.ucode)) {
      app.api().Get('openapi/v1/getCodeUser', {ucode:app.globalData.ucode }, (rel) => {
        if (rel.code == 200) {
          this.setData({
            ucode: app.globalData.ucode,
            ucode_user: rel.data,
            show: true,
          })
        } else {
          this.setData({
            ucode_user: {face: '/tab/img/nickname.png',nickname:'匿名'}
          })
        }
      })
    }
  },
  //输入邀请码
  inputCode: function (event) {
    var cursor = event.detail.cursor, code = event.detail.value;
    if (cursor >= 4) {
      app.globalData.ucode = code;
      this.getUcode();
    } else {
      this.setData({
        ucode_user: {face: '/tab/img/nickname.png',nickname:'匿名'}
      })
    }
  },
  /**
   * 关闭弹出窗口
   */
  onClose: function () {
    this.setData({
      show: !this.data.show
    })
  },
  /**
   * 用户登录
   */
  authorLogin: function (e) {
    if (e.detail.errMsg !== 'getUserInfo:ok') {
      return false;
    }
    wx.showLoading({
      title: '正在登录',
    })
    app.doLogin(e,rel => {
      wx.hideLoading()
      if(rel){
        this.notlogin();
      }else{
        app.wxAlert('登录授权失败,请重新登录')
      }
    });
  },
  //返回页面
  notlogin() {
    var pages = getCurrentPages();
    if(pages.length == 2){
      wx.navigateBack({
        delta:1,
      })
    }else{
      wx.switchTab({
        url: '/tab/index',
      })
    }
  }
})